using Coldairarrow.Entity.BTB_StaffUsers;
using Coldairarrow.Util;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Coldairarrow.Api.Controllers.SubApp
{
    [Route("SubApp/[controller]/[action]")]
    public class UserSyncController : BaseApiController
    {

        readonly IUserSyncBusiness _userBus;
        readonly IFrontUserSyncBusiness _frontUserSyncBus;
        readonly IDepartmentSyncBusiness _departmentBus;
        #region DI
        public UserSyncController(IUserSyncBusiness userBus, IFrontUserSyncBusiness frontUserSyncBus, IDepartmentSyncBusiness departmentBus)
        {
            _userBus = userBus;
            _frontUserSyncBus = frontUserSyncBus;
            _departmentBus = departmentBus;
        }
        #endregion


        #region 提交
        /// <summary>
        /// 添加用户
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        [CheckBasicAuth]
        public async Task<ActionResult> Hook()
        {
            string json = await Request.Body.ReadToStringAsync();
            string key = "x-zhike-event";
            Request.Headers.TryGetValue(key, out var eventType);
            string type = eventType[0];
            switch (type)
            {
                case "BackUserCreate":
                    await this.BackUserCreate(json);
                    break;
                case "BackUserUpdate":
                    await this.BackUserUpdate(json);
                    break;
                case "BackUserDelete":
                    await this.BackUserDelete(json);
                    break;
                case "BackUserBulk":
                    await this.BackUserBulk(json);
                    break;
                case "FrontUserCreate":
                    await this.FrontUserCreate(json);
                    break;
                case "FrontUserUpdate":
                    await this.FrontUserUpdate(json);
                    break;
                case "FrontUserBulk":
                    await this.FrontUserBulk(json);
                    break;
                case "GetRoleList":
                    return await this.GetRoleList();
                case "DepartmentCreate":
                    await this.DepartmentCreate(json);
                    break;
                case "DepartmentUpdate":
                    await this.DepartmentUpdate(json);
                    break;
                case "DepartmentDelete":
                    await this.DepartmentDelete(json);
                    break;
                case "DepartmentBulk":
                    await this.DepartmentBulk(json);
                    break;
                default:
                    break;
            }
            return Ok();
        }

        
        #endregion

        #region 私有方法
        /// <summary>
        /// 添加用户
        /// </summary>
        /// <returns></returns>
        private async Task BackUserCreate(string json)
        {
            UserSyncInputDTO input = json.ToObject<UserSyncInputDTO>();
            await _userBus.AddDataAsync(input);
        }

        /// <summary>
        /// 修改用户
        /// </summary>
        /// <returns></returns>
        private async Task BackUserUpdate(string json)
        {
            UserSyncInputDTO input = json.ToObject<UserSyncInputDTO>();
            await _userBus.UpdateDataAsync(input);
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="json"></param>
        /// <returns></returns>
        private async Task BackUserDelete(string json)
        {
            List<string> ids = json.ToObject<List<string>>();
            await _userBus.DeleteDataAsync(ids);
        }

        /// <summary>
        ///  批量同步用户
        /// </summary>
        /// <param name="json"></param>
        /// <returns></returns>
        private async Task BackUserBulk(string json)
        {
            List<UserSyncInputDTO> list = json.ToObject<List<UserSyncInputDTO>>();
            foreach (var item in list)
            {
                if (item.Id == GlobalData.ADMINID)
                    continue;

                if (_userBus.CheckExists(item.Id))
                {
                    await _userBus.UpdateDataAsync(item);
                }
                else
                {
                    await _userBus.AddDataAsync(item);
                }
            }
        }

        /// <summary>
        /// 添加用户
        /// </summary>
        /// <returns></returns>
        private async Task FrontUserCreate(string json)
        {
            var input = json.ToObject<BTB_StaffUser>();
            await _frontUserSyncBus.AddDataAsync(input);
        }

        /// <summary>
        /// 修改用户
        /// </summary>
        /// <returns></returns>
        private async Task FrontUserUpdate(string json)
        {
            var input = json.ToObject<BTB_StaffUser>();
            await _frontUserSyncBus.UpdateDataAsync(input);
        }

        /// <summary>
        ///  批量同步用户
        /// </summary>
        /// <param name="json"></param>
        /// <returns></returns>
        private async Task FrontUserBulk(string json)
        {
            List<BTB_StaffUser> list = json.ToObject<List<BTB_StaffUser>>();
            foreach (var item in list)
            {
                if (_frontUserSyncBus.CheckExists(item.id))
                {
                    await _frontUserSyncBus.UpdateDataAsync(item);
                }
                else
                {
                    await _frontUserSyncBus.AddDataAsync(item);
                }
            }
        }

        /// <summary>
        /// 返回角色列表
        /// </summary>
        /// <returns></returns>
        private async Task<ActionResult> GetRoleList()
        {
            var list = await _userBus.GetRoleListAsync();
            var result = new AjaxResult<List<RoleDTo>>
            {
                Success = true,
                Data = list
            };
            return Content(result.ToJson());
        }

        /// <summary>
        /// 批量同步部门
        /// </summary>
        /// <param name="json"></param>
        /// <returns></returns>
        private async Task DepartmentBulk(string json)
        {
            List<DepartmentSyncInputDTO> list = json.ToObject<List<DepartmentSyncInputDTO>>();
            List<string> ids = await _departmentBus.GetAllDataAsync();
            var deleteIds = ids.Where( x=> !list.Select(y => y.Id).ToList().Contains(x)).ToList();
            await _departmentBus.DeleteDataAsync(deleteIds);
            foreach (var item in list)
            {
                if (_departmentBus.CheckExists(item.Id))
                {
                    await _departmentBus.UpdateDataAsync(item);
                }
                else
                {
                    await _departmentBus.AddDataAsync(item);
                }
            }
        }

        /// <summary>
        /// 删除部门
        /// </summary>
        /// <param name="json"></param>
        /// <returns></returns>
        private async Task DepartmentDelete(string json)
        {
            List<string> ids = json.ToObject<List<string>>();
            await _departmentBus.DeleteDataAsync(ids);
        }

        /// <summary>
        /// 更新部门
        /// </summary>
        /// <param name="json"></param>
        /// <returns></returns>
        private async Task DepartmentUpdate(string json)
        {
            DepartmentSyncInputDTO input = json.ToObject<DepartmentSyncInputDTO>();
            if (_departmentBus.CheckExists(input.Id))
            {
                await _departmentBus.UpdateDataAsync(input);
            }
            else
            {
                await _departmentBus.AddDataAsync(input);
            }
        }

        /// <summary>
        /// 新建部门
        /// </summary>
        /// <param name="json"></param>
        /// <returns></returns>
        private async Task DepartmentCreate(string json)
        {
            DepartmentSyncInputDTO input = json.ToObject<DepartmentSyncInputDTO>();
            await _departmentBus.AddDataAsync(input);
        }
        #endregion
    }
}
